package com.ruoyi.web.service.Impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.mapper.MesMakeVersionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.mapper.MesReplenishMapper;
import com.ruoyi.web.service.IMesReplenishService;

import javax.annotation.Resource;

/**
 * 补版管理Service业务层处理
 *
 * @author levicash
 * @date 2024-04-12
 */
@Service
public class MesReplenishServiceImpl implements IMesReplenishService {
    @Resource
    private MesReplenishMapper mesReplenishMapper;

    @Resource
    private MesMakeVersionMapper mesMakeVersionMapper;

    /**
     * 查询补版管理
     *
     * @param id 补版管理主键
     * @return 补版管理
     */
    @Override
    public MesReplenish selectMesReplenishById(Long id) {
        return mesReplenishMapper.selectMesReplenishById(id);
    }

    /**
     * 查询补版管理列表
     *
     * @param mesReplenish 补版管理
     * @return 补版管理
     */
    @Override
    public List<MesReplenish> selectMesReplenishList(MesReplenish mesReplenish) {
        return mesReplenishMapper.selectMesReplenishList(mesReplenish);
    }

    @Override
    public List<MesReplenishDetail> getDetailList(MesReplenishDetail mesReplenishDetail) {
        return mesReplenishMapper.getDetailList(mesReplenishDetail);
    }


    /**
     * 新增补版管理
     *
     * @param mesReplenish 补版管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesReplenish(MesReplenish mesReplenish) {
        mesReplenish.setCreateTime(DateUtils.getNowDate());
        mesReplenish.setCreateBy(SecurityUtils.getUsername());
        // 生成制版单编号
        int maxNum = mesReplenishMapper.selectMaxId();
        String code = PinYinUtils.getCodeNormal(maxNum, "BBGL"); // 补版管理
        mesReplenish.setCode(code);

        int rows = mesReplenishMapper.insertMesReplenish(mesReplenish);
        insertMesReplenishDetail(mesReplenish);
        return rows;
    }

    /**
     * 修改补版管理
     *
     * @param mesReplenish 补版管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesReplenish(MesReplenish mesReplenish) {
        mesReplenish.setUpdateTime(DateUtils.getNowDate());
        mesReplenish.setUpdateBy(SecurityUtils.getUsername());

//        if (mesReplenish.getStatus() == 2) {//提交
//
//        } else {
//            mesReplenishMapper.deleteMesReplenishDetailByReplenishId(mesReplenish.getId());
//            insertMesReplenishDetail(mesReplenish);
//        }
        return mesReplenishMapper.updateMesReplenish(mesReplenish);
    }

    /**
     * 批量删除补版管理
     *
     * @param ids 需要删除的补版管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesReplenishByIds(Long[] ids) {
        mesReplenishMapper.deleteMesReplenishDetailByReplenishIds(ids);
        return mesReplenishMapper.deleteMesReplenishByIds(ids);
    }

    /**
     * 删除补版管理信息
     *
     * @param id 补版管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesReplenishById(Long id) {
        mesReplenishMapper.deleteMesReplenishDetailByReplenishId(id);
        return mesReplenishMapper.deleteMesReplenishById(id);
    }

    /**
     * 新增补版管理明细表信息
     *
     * @param mesReplenish 补版管理对象
     */
    public void insertMesReplenishDetail(MesReplenish mesReplenish) {
        List<MesReplenishDetail> mesReplenishDetailList = mesReplenish.getMesReplenishDetailList();
        Long id = mesReplenish.getId();
        if (StringUtils.isNotNull(mesReplenishDetailList)) {
            List<MesReplenishDetail> list = new ArrayList<MesReplenishDetail>();
            int xh = 0;
            for (MesReplenishDetail mesReplenishDetail : mesReplenishDetailList) {
                mesReplenishDetail.setReplenishId(id);
                list.add(mesReplenishDetail);
                //补版序列号
                xh = xh + 1;
                String xlh = PinYinUtils.hundred(xh);//序列号
                mesReplenishDetail.setReplenishNo(mesReplenish.getCode() + "BB" + xlh);
            }
            if (list.size() > 0) {
                mesReplenishMapper.batchMesReplenishDetail(list);
            }
        }
    }
}
